package com.tanhua.server.controller;

import com.tanhua.model.dto.CommentDto;
import com.tanhua.model.vo.PageResult;
import com.tanhua.server.service.CommentsService;
import com.tanhua.server.service.MovementService;
import com.tanhua.server.service.SmallVideosService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @author wjs
 * @version 1.0
 * @date 2022/1/2 0002 下午 6:33
 */

@RestController
@RequestMapping("/smallVideos")
public class SmallVideosController {

    @Autowired
    private SmallVideosService videosService;

    @Autowired
    private MovementService movementService;

    @Autowired
    private CommentsService commentsService;

    /**
     * 发布视频
     * @param videoThumbnail 视频封面
     * @param videoFile      视频文件
     * @return
     * @throws IOException
     */
    @PostMapping
    public ResponseEntity saveVideos(MultipartFile videoThumbnail, MultipartFile videoFile) throws IOException {

        videosService.saveVideos(videoThumbnail,videoFile);

        return ResponseEntity.ok(null);
    }

    /**
     * 视频分页列表
     * @param page
     * @param pagesize
     * @return
     */
    @GetMapping
    public ResponseEntity queryVideoList(@RequestParam(defaultValue = "1")  Integer page,
                                          @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult pageResult = videosService.queryVideoList(page, pagesize);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 视频点赞
     * @param id
     * @return
     */
    @PostMapping("/{id}/like")
    public ResponseEntity like(@PathVariable("id") String id){
        movementService.addLike(id);
        return ResponseEntity.ok(null);
    }

    /**
     * 视频点赞 - 取消
     * @param id
     * @return
     */
    @PostMapping("/{id}/dislike")
    public ResponseEntity disLike(@PathVariable("id") String id){
        movementService.disLike(id);
        return ResponseEntity.ok(null);
    }

    /**
     * 视频用户关注
     * @param uid
     * @return
     */
    @PostMapping("/{uid}/userFocus")
    public ResponseEntity userFocus(@PathVariable("uid") String uid){
        videosService.saveFocus(uid);
        return ResponseEntity.ok(null);
    }

    /**
     * 视频用户关注 - 取消
     * @param uid
     * @return
     */
    @PostMapping("/{uid}/userUnFocus")
    public ResponseEntity userUnFocus(@PathVariable("uid") String uid){
        videosService.removeFocus(uid);
        return ResponseEntity.ok(null);
    }

    /**
     * 视频评论列表
     * @param page
     * @param pagesize
     * @param id
     * @return
     */
    @GetMapping("/{id}/comments")
    public ResponseEntity comments(@RequestParam(defaultValue = "1") Integer page,
                                   @RequestParam(defaultValue = "10") Integer pagesize,
                                   @PathVariable String id){
        PageResult pageResult = commentsService.findComments(id,page,pagesize);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 视频评论发布
     * @param commentDto
     * @param id
     * @return
     */
    @PostMapping("/{id}/comments")
    public ResponseEntity saveComments(@RequestBody CommentDto commentDto,
                                       @PathVariable("id") String id){
        commentDto.setMovementId(id);
        commentsService.saveComments(commentDto);
        return ResponseEntity.ok(null);
    }
}
